﻿using System;
using System.Configuration;
using System.Data;
using System.Text;
using Common;
using HCRS.DBACL;
using HCRS.Errors;
using HCRS.Implementation;

namespace JinJiang.BookingEngine.Web.en
{
public class EnSearchHotelClass
{
    private CommonWeb _CommonClass = new CommonWeb();
    private DataAccessLayer _DataAccessLayer = new DataAccessLayer();
    private Functions _Functions = new Functions();
    private HotelClass _HotelClass = new HotelClass();
    private string[] mapKey = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    public static int PageCount;
    public string photourl = ConfigurationManager.AppSettings["photourl"].ToString();
    public string ratecode = ConfigurationManager.AppSettings["ratecode"].ToString();
    public static int TotalRecord;

    private string getAmenity(string amenity, int lan)
    {
        StringBuilder sbamenity = new StringBuilder();
        if (lan == 1)
        {
            if ((amenity.IndexOf("* Business Center") > 0) || (amenity.IndexOf("* 商务中心") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/ABusiness.gif' hspace='2' title='Business Center'/>");
            }
            if (((amenity.IndexOf("* Dinner") > 0) || (amenity.IndexOf("* Restaurant") > 0)) || (amenity.IndexOf("* 餐厅") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/ARestaurant.gif' hspace='2' title='Dinner'/>");
            }
            if ((amenity.IndexOf("* Beauty Salon") > 0) || (amenity.IndexOf("* Spa水疗护理中心") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/ASpa.gif' hspace='2' title='Beauty Salon'/>");
            }
            if (((amenity.IndexOf("* Exercise Gym") > 0) || (amenity.IndexOf("* 健身中心") > 0)) || (amenity.IndexOf("* 配有运动场所") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/AFitness.gif' hspace='2' title=' Exercise Gym'/>");
            }
            if (((amenity.IndexOf("High Speed Internet") > 0) || (amenity.IndexOf("宽带上网") > 0)) || (amenity.IndexOf("因特网接口") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/AInternet.gif' hspace='2' title='High Speed Internet'/>");
            }
            if (amenity.IndexOf("* 室内游泳池") > 0)
            {
                sbamenity.Append("<img src='../Images/hotel/APool.gif' hspace='2' title='室内游泳池'/>");
            }
        }
        else
        {
            if ((amenity.IndexOf("* Business Center") > 0) || (amenity.IndexOf("* 商务中心") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/ABusiness.gif' hspace='2' alt='商务中心'/>");
            }
            if (((amenity.IndexOf("* Dinner") > 0) || (amenity.IndexOf("* Restaurant") > 0)) || (amenity.IndexOf("* 餐厅") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/ARestaurant.gif' hspace='2' alt='餐厅'/>");
            }
            if ((amenity.IndexOf("* Beauty Salon") > 0) || (amenity.IndexOf("* Spa水疗护理中心") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/ASpa.gif' hspace='2' alt='Spa'/>");
            }
            if (((amenity.IndexOf("* Exercise Gym") > 0) || (amenity.IndexOf("* 健身中心") > 0)) || (amenity.IndexOf("* 配有运动场所") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/AFitness.gif' hspace='2' alt='健身中心'/>");
            }
            if (((amenity.IndexOf("* High Speed Internet") > 0) || (amenity.IndexOf("* 宽带上网") > 0)) || (amenity.IndexOf("* 因特网接口") > 0))
            {
                sbamenity.Append("<img src='../Images/hotel/AInternet.gif' hspace='2' alt='宽带上网'/>");
            }
            if (amenity.IndexOf("* 室内游泳池") > 0)
            {
                sbamenity.Append("<img src='../Images/hotel/APool.gif' hspace='2' alt='室内游泳池'/>");
            }
        }
        return sbamenity.ToString();
    }

    public string GetGuaImage(string gua)
    {
        string str = "";
        string guaOK = gua;
        if (guaOK == null)
        {
            return str;
        }
        if (!(guaOK == "PP") && !(guaOK == "DP"))
        {
            if (guaOK != "CC")
            {
                return str;
            }
        }
        else
        {
            return "<img src=\"images/money.gif\" width=\"11\" height=\"11\" title=\"本房型需预付保证\" />";
        }
        return "<img src=\"images/visa.gif\" width=\"11\" height=\"11\"/>";
    }

    public string GetHotels(string hotels, int gid)
    {
        string hotelsOK;
        try
        {
            DataTable dt = CommonDataAccess.GetTable("select H.*,D.Amenities from (select  pid as P, Hotel_ID, replace(Title,'\"','&quot;') as Title, pid,keywords, Stars, Diamond,replace(convert(nvarchar(4000),subtitle),'\"','&quot;') as subtitle, LowPrice, highprice, photourl, replace(Address1,'\"','&quot;') as Address1, citygbid, districtid, commercialid, localLongitude, localLatitude,GroupID,pagerank  from hotel where  Passed=1 and  pid in(select  pid  from hotel where groupid=" + gid + ")) H left join   H_description  D on H.pid=D.property_id and d.languagetype=0  order by pagerank desc", "hotel");
            StringBuilder str = new StringBuilder(hotels + "={");
            StringBuilder temp = new StringBuilder();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str.Append("P" + dt.Rows[i][0].ToString() + ":[");
                for (int c = 1; c < (dt.Columns.Count - 1); c++)
                {
                    str.Append("\"" + dt.Rows[i][c].ToString().Replace(Convert.ToChar(0).ToString(), "").Replace(Convert.ToChar(13).ToString(), "").Replace(Convert.ToChar(10).ToString(), "") + "\",");
                }
                str.Append("\"" + this.getAmenity(dt.Rows[i][dt.Columns.Count - 1].ToString(), 0).Replace("\n", "") + "\"],");
            }
            str.Append("P:[0,0]};");
            hotelsOK = str.ToString().Replace(Convert.ToChar(0).ToString(), "");
        }
        catch (Exception e)
        {
            WebError.SendErrorEmail(e);
            throw e;
        }
        return hotelsOK;
    }

    public DataTable GetHotelsByPID(string hotels, string PIDS, int lan)
    {
        DataTable dt = CommonDataAccess.GetTable("select Isnull(Title_e,replace(Title_e,'\"','&quot;')) as title,Isnull(Address1_e,replace(Address1_e,'\"','&quot;')) as address,h_city.remark as city,pid,stars,hotel.photourl,hotel.citygbid from hotel left join h_city on hotel.citygbid=h_city.citygbid where pid in (" + PIDS + ") order by hotel.citygbid, stars desc  ", "hotel");
        if ((dt != null) && (dt.Rows.Count > 0))
        {
            return dt;
        }
        return null;
    }

    public string GetPrices(string array, int gid, string RateCode, string checkIN, string checkOUT)
    {
        DataTable dt = CommonDataAccess.GetTable(string.Concat(new object[] { "select distinct  Property_id,PriceNoTax from h_cache where      avstat!='N' and priceNoTax>0  and (datediff(hh,getdate(),Availdate)+24>=cutoff)  and  Availdate>= '", checkIN, "' AND Availdate<'", checkOUT, "'    and  RateCode in(", RateCode, ")     and Property_id in(select  pid  from hweb_hotel where gid=", gid, ") order by Property_id,PriceNoTax " }), "price");
        StringBuilder str = new StringBuilder(array + "=[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            str.Append("[" + dt.Rows[i][0].ToString() + "," + dt.Rows[i][1].ToString() + "],");
        }
        str.Append("[0,0]");
        str.Append("];");
        return str.ToString();
    }

    private string getStar(string star)
    {
        StringBuilder sbStar = new StringBuilder();
        sbStar.Append("<img src='");
        if (star == "0")
        {
            sbStar.Append("../Images/hotel/value.gif");
        }
        else
        {
            sbStar.Append("../Images/hotel/" + star + "star.gif");
        }
        sbStar.Append("'/>");
        return sbStar.ToString();
    }

    public string pagination(int total, int per, int page, string query_string)
    {
        int allpage = 0;
        int next = 0;
        int pre = 0;
        int startcount = 0;
        int endcount = 0;
        string pagestr = "<br/>";
        if (page < 1)
        {
            page = 1;
        }
        if (per != 0)
        {
            allpage = total / per;
            allpage = ((total % per) != 0) ? (allpage + 1) : allpage;
            allpage = (allpage == 0) ? 1 : allpage;
        }
        next = page + 1;
        pre = page - 1;
        startcount = ((page + 5) > allpage) ? (allpage - 9) : (page - 4);
        endcount = (page < 5) ? 10 : (page + 5);
        object allOK = (pagestr + "<table width=\"98%\"  border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\">") + "<tr>" + "<td width=\"50%\" height=\"30\">";
        pagestr = string.Concat(new object[] { allOK, "<div >第", page, "页，总", allpage, "页，点击页码查看下一页</strong></div>" }) + "<td width=\"50%\" valign=\"middle\">" + "<div align=\"right\">";
        if (startcount < 1)
        {
            startcount = 1;
        }
        if (allpage < endcount)
        {
            endcount = allpage;
        }
        pagestr = pagestr + ((page > 1) ? ("<a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(1)\" ><img src=\"../Images/hotel/nav_first.gif\" width=\"15\" height=\"10\" hspace=\"5\" border=\"0\"></a><a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(" + pre + ")\" ><img src=\"../Images/hotel/nav_left.gif\" hspace=\"5\" border=\"0\"></a>") : "<img src=\"../Images/hotel/nav_first1.gif\" width=\"15\" height=\"10\" hspace=\"5\"><img src=\"../Images/hotel/nav_left1.gif\" hspace=\"5\">");
        for (int i = startcount; i <= endcount; i++)
        {
            pagestr = pagestr + ((page == i) ? ("&nbsp;&nbsp;<font color=\"#ff0000\">" + i + "</font>") : string.Concat(new object[] { "&nbsp;&nbsp;<a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(", i, ")\" >", i, "</a>" }));
        }
        return ((pagestr + ((page != allpage) ? string.Concat(new object[] { "&nbsp;<a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(", next, ")\" ><img src=\"../Images/hotel/nav_right.gif\" hspace=\"5\" border=\"0\"></a><a href=\"javascript:void(0);\"  onclick=\"SearchHotelPage(", allpage, ")\" ><img src=\"../Images/hotel/nav_last.gif\" width=\"15\" height=\"10\" hspace=\"5\" border=\"0\"></a>" }) : "<img src=\"../Images/hotel/nav_right1.gif\" hspace=\"5\"><img src=\"../Images/hotel/nav_last1.gif\" width=\"15\" height=\"10\" hspace=\"5\" border=\"0\">") + "</div>") + "</td></tr>" + "</table>");
    }

    public bool RateCodeAvailable(int hotelID, string PorH, string rateCode, out string availableRateCode)
    {
        string allratecode = "";
        DataTable dt = null;
        if (PorH == "hid")
        {
            dt = CommonDataAccess.GetTable("select RateCode from CRM_hotelweb_domain where property_id=(select pid from hotel where hotel_id=" + hotelID.ToString() + ") and languagetype=1", "tab");
        }
        else
        {
            dt = CommonDataAccess.GetTable("select RateCode from CRM_hotelweb_domain where property_id=" + hotelID.ToString() + " and languagetype=1", "tab");
        }
        try
        {
            allratecode = dt.Rows[0]["RateCode"].ToString();
        }
        catch
        {
            allratecode = this.ratecode;
        }
        string TempRateClass = allratecode.Replace("|", ",");
        if ((rateCode != "") && (rateCode != null))
        {
            if (("," + TempRateClass + ",").IndexOf(rateCode + ",") >= 0)
            {
                availableRateCode = rateCode;
                return true;
            }
            availableRateCode = "";
            return false;
        }
        availableRateCode = allratecode;
        return true;
    }
}

}